package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.*;
import com.aftersale.demo.mapper.RolePermissionMapper;
import com.aftersale.demo.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.service.RoleService;
import com.aftersale.demo.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public Result getRolePermission(Integer pageNum, Integer pageSize) {
        roleMapper.selectList(null);
        Page<Role> rolePage=page(new Page<>(pageNum,pageSize));
        List<Role> roleList=rolePage.getRecords();
        for (Role role : roleList) {
            List<Permission> permissionList=roleMapper.selectRolePermission(role.getId());
            role.setRolePermission(permissionList);
        }
        return Result.success("请求成功",rolePage);
    }


    @Override
    public Result addRole(Role role) {
        LambdaQueryWrapper<Role> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleName,role.getRoleName());
        Role dbRole=roleMapper.selectOne(queryWrapper);
        if(!Objects.isNull(dbRole)){
            return Result.error("401","角色已存在");
        }
        roleMapper.insert(role);
        dbRole=roleMapper.selectOne(queryWrapper);
        if(!Objects.isNull(role.getRolePermission())){

            for (Permission permission : role.getRolePermission()) {
                rolePermissionMapper.manageRolePermission(dbRole.getId(),permission.getId());
            }
        }else{
            rolePermissionMapper.manageRolePermission(dbRole.getId(),19);
        }

        return Result.success("添加角色成功");
    }

    @Override
    public Result deleteRole(int roleId) {

        if(roleId==1||roleId==2||roleId==3||roleId==4){
            return Result.error("401","系统必要角色，不允许删除！");
        }
        List<User> userList=userMapper.selectRoleUser(roleId);
        if(userList.size()==0){
            roleMapper.deleteById(roleId);
            return Result.success("删除成功");
        }
        else{
            return Result.error("401","删除失败，请先删除角色对应的所有用户");
        }
    }

    @Override
    public Result updateRole(Role role) {
        LambdaQueryWrapper<Role> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getRoleName,role.getRoleName());
        Role dbRole=roleMapper.selectOne(queryWrapper);
        if(!Objects.isNull(dbRole)){
            return Result.error("401","角色已存在");
        }
        roleMapper.updateById(role);
        return Result.success("修改成功");
    }

    @Override
    public Result getRole() {
        return Result.success("",roleMapper.selectList(null));
    }

    @Override
    public Result selectRolePermission(String roleName, Integer pageNum, Integer pageSize) {
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("role_name",roleName);
        Page<Role> rolePage=page(new Page<>(pageNum,pageSize),queryWrapper);
        List<Role> roleList=rolePage.getRecords();
        for (Role role : roleList) {
            List<Permission> permissionList=roleMapper.selectRolePermission(role.getId());
            role.setRolePermission(permissionList);
        }
        return Result.success("请求成功",rolePage);
    }


}




